package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.AucBidhistoryDto;
import com.tianqu.tms.dto.AucManagerDto;
/**
 * 
 * 竞价历史记录实现类
 * 
 * @author Administrator
 *
 */
public class AucBidhistoryDaoImp extends TmsDao implements AucBidhistoryDao {

	protected AucBidhistoryDaoImp() throws TmsDaoException {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * 根据会员ID查询车辆竞价信息
	 */
	@Override
	public List<AucBidhistoryDto> findAucHistoryByAucId(int memberId,int aucMagId)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<AucBidhistoryDto> dtoList = new ArrayList<AucBidhistoryDto>();
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append("ID		 ,");
		sql.append("AUCMAGID ,");
		sql.append("MEMBERID ,");
		sql.append("BIDTIME  ,");
		sql.append("PRICE     ");
		sql.append("FROM ");
		sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
		sql.append("   WHERE MEMBERID =?  ");
		sql.append("   AND AUCMAGID =?  ");
		sql.append("   ORDER BY  BIDTIME DESC  ");
		AucBidhistoryDto dto = null;
		try {
			
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, memberId);
			ps.setInt(2, aucMagId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dto = new AucBidhistoryDto();
				dto.setId(rs.getInt("ID"));
				dto.setAucMagId(rs.getInt("AUCMAGID"));
				dto.setMemberId(rs.getString("MEMBERID"));
				dto.setBidTime(rs.getString("BIDTIME"));
				dto.setPrice(rs.getInt("PRICE"));
				dtoList.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		return dtoList;
	}

	/**
	 * 根据竞价车辆管理表主键ID查询竞价车辆信息
	 */
	@Override
	public List<AucBidhistoryDto> getAucBidInfoByAucId(int aucId)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<AucBidhistoryDto> dtoList = new ArrayList<AucBidhistoryDto>();
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append("ID		 ,");
		sql.append("AUCMAGID ,");
		sql.append("MEMBERID ,");
		sql.append("BIDTIME  ,");
		sql.append("PRICE     ");
		sql.append("FROM ");
		sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
		sql.append("   WHERE AUCMAGID =?  ");
		sql.append("   AND (  ");
		sql.append("   MEMBERID IN (SELECT MEMBERID FROM ");
		sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
		sql.append("   WHERE AUCMAGID =?  ");
		sql.append("    GROUP BY MEMBERID HAVING COUNT(MEMBERID) = 1)  ");
		sql.append("   OR  ");
		sql.append("    ID IN (SELECT MAX(ID) FROM ");
		sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
		sql.append("   WHERE AUCMAGID =?  ");
		sql.append("   GROUP BY MEMBERID HAVING COUNT(MEMBERID)>1))  ");
		sql.append("   ORDER BY PRICE DESC,  BIDTIME ASC  ");//竞价历史信息更改 modify by mufeiyu
		AucBidhistoryDto dto = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, aucId);
			ps.setInt(2, aucId);
			ps.setInt(3, aucId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dto = new AucBidhistoryDto();
				dto.setId(rs.getInt("ID"));
				dto.setAucMagId(rs.getInt("AUCMAGID"));
				dto.setMemberId(rs.getString("MEMBERID"));
				dto.setBidTime(rs.getString("BIDTIME"));
				dto.setPrice(rs.getInt("PRICE"));
				dtoList.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		return dtoList;
	}

	
	/**
	 * 
	 * 会员竞价出价添加一条出价记录
	 */
	@Override
	public void saveAucHisInfoByDto(AucBidhistoryDto dto)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			StringBuffer sql = new StringBuffer("INSERT INTO ");
			sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
			sql.append("(");
			sql.append(" AUCMAGID, ");
			sql.append(" MEMBERID, ");
			sql.append(" BIDTIME,  ");
			sql.append(" PRICE     ");
			sql.append("  ) VALUES ( ");
			sql.append(" ?,?,SYSDATE(),?)");

			ps = conn.prepareStatement(sql.toString());
			
			ps.setInt(1, dto.getAucMagId());
			ps.setString(2, dto.getMemberId());
			ps.setInt(3, dto.getPrice());

			ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		
	}
	/**
	 * 根据竞价车辆管理表主键ID查询竞价车辆信息
	 */
	@Override
	public List<AucBidhistoryDto> getAucBidInfoByAucIdAndPrice(int aucId)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<AucBidhistoryDto> dtoList = new ArrayList<AucBidhistoryDto>();
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append("ID		 ,");
		sql.append("AUCMAGID ,");
		sql.append("MEMBERID ,");
		sql.append("BIDTIME  ,");
		sql.append("PRICE     ");
		sql.append("FROM ");
		sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
		sql.append("   WHERE AUCMAGID =?  ");
		sql.append("   ORDER BY  PRICE DESC  ");
		AucBidhistoryDto dto = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, aucId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dto = new AucBidhistoryDto();
				dto.setId(rs.getInt("ID"));
				dto.setAucMagId(rs.getInt("AUCMAGID"));
				dto.setMemberId(rs.getString("MEMBERID"));
				dto.setBidTime(rs.getString("BIDTIME"));
				dto.setPrice(rs.getInt("PRICE"));
				dtoList.add(dto);
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
		return dtoList;
	}

	/**
	 * 竞价交易信息完成，插入历史信息记录
	 */
	@Override
	public void setCarBidHisttory(AucBidhistoryDto dto)
			throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO  ");
		sql.append(TmsTableNames.TMS_AUC_BIDHISTORY);
		sql.append(" ( ");
		sql.append(" AUCMAGID,");	
		sql.append(" MEMBERID,");	
		sql.append(" BIDTIME,");		
		sql.append(" PRICE ");	
		sql.append(" ) VALUES ");
		sql.append("(?,?,?,?)");
		try {
				ps = conn.prepareStatement(sql.toString());
				ps.setInt(1, dto.getAucMagId());
				ps.setString(2, dto.getMemberId());
				ps.setString(3, dto.getBidTime());
				ps.setInt(4, dto.getPrice());
				ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			close(rs, ps);
		}		
		
	}
}
